package sea;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import sea.util.FileHelper;
import sea.util.PublicParam;


/**
 * 
 * This class is used to build the New Model Form
 * 
 * @author  Zhirun Ding
 * 
 * Created on 03/07/2012
 * 
 */

public class NewModelForm extends javax.swing.JDialog {
    
    /** Creates new form NewModelForm */
    public NewModelForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        modelLanguageButtonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        modelNameText = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        pathLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        flatRadioButton = new javax.swing.JRadioButton();
        pepaRadioButton = new javax.swing.JRadioButton();
        hypeRadioButton = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sea.SimHyAGui.class).getContext().getResourceMap(NewModelForm.class);
        setTitle(resourceMap.getString("NewModelForm.title")); // NOI18N
        setName("NewModelForm"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        modelNameText.setText(resourceMap.getString("modelNameText.text")); // NOI18N
        modelNameText.setName("modelNameText"); // NOI18N

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        pathLabel.setText(resourceMap.getString("pathLabel.text")); // NOI18N
        pathLabel.setName("pathLabel"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        modelLanguageButtonGroup.add(flatRadioButton);
        flatRadioButton.setSelected(true);
        flatRadioButton.setText(resourceMap.getString("flatRadioButton.text")); // NOI18N
        flatRadioButton.setName("flatRadioButton"); // NOI18N

        modelLanguageButtonGroup.add(pepaRadioButton);
        pepaRadioButton.setLabel(resourceMap.getString("pepaRadioButton.label")); // NOI18N
        pepaRadioButton.setName("pepaRadioButton"); // NOI18N

        modelLanguageButtonGroup.add(hypeRadioButton);
        hypeRadioButton.setText(resourceMap.getString("hypeRadioButton.text")); // NOI18N
        hypeRadioButton.setName("hypeRadioButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pathLabel)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(flatRadioButton)
                                    .addComponent(modelNameText, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(pepaRadioButton)
                                    .addComponent(hypeRadioButton))))
                        .addGap(30, 30, 30))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cancelButton)
                        .addGap(53, 53, 53)
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pathLabel)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(modelNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(flatRadioButton))
                .addGap(18, 18, 18)
                .addComponent(pepaRadioButton)
                .addGap(18, 18, 18)
                .addComponent(hypeRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        PublicParam.NewModelFileName = "";
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        try {
            //check the model name
            String modelName = modelNameText.getText();
            if (modelName.length() == 0) {
                JOptionPane.showMessageDialog(this, "Model name is empty,pleases input!", "Error", JOptionPane.ERROR_MESSAGE);
                modelNameText.requestFocus();
                return;
            }

            //model type
            String modelType = "";
            if (flatRadioButton.isSelected()) modelType="flat";
            if (hypeRadioButton.isSelected()) modelType="hype";
            if (pepaRadioButton.isSelected()) modelType="biopepa";
            
            //new txt file
            String fileName = modelName + "." + modelType;
            String filePath = pathLabel.getText() + "\\" + fileName + ".txt";
            if (!FileHelper.createTxtFile(filePath)) {
                JOptionPane.showMessageDialog(this, "Model name[" + modelNameText.getText() + "] has existed, pls input other model name!" , "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            //copy template file of model
            String templatePath = System.getProperty("user.dir") + "\\" + modelType + ".template";
            FileHelper.copyFile(templatePath, filePath);
            
            //close the form
            PublicParam.NewModelFileName = fileName;
            this.dispose();
        } catch (IOException ex) {
            Logger.getLogger(NewModelForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_saveButtonActionPerformed
    
    public void setPathToLabel(String path) {
        pathLabel.setText(path);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewModelForm dialog = new NewModelForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JRadioButton flatRadioButton;
    private javax.swing.JRadioButton hypeRadioButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.ButtonGroup modelLanguageButtonGroup;
    private javax.swing.JTextField modelNameText;
    private javax.swing.JLabel pathLabel;
    private javax.swing.JRadioButton pepaRadioButton;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables
    
}
